<?php
namespace app\app\controller;

use app\common\controller\App;
use think\Db;
use think\Exception;


class Comment extends App
{
    protected $middleware = ['AppAuth'];

    public function add()
    {

        $comment=request()->param("comment");

        $data['images']=request()->param("images");
        $data['order_id']=request()->param("order_id",'0','intval');
        $data['score']=request()->param("score",'0','intval');
        $data['content']=request()->param("content");
        $data['uid']=$this->getLoginUid();
        $data['create_time']=time();
        $data['images']=is_array($data['images'])?implode(',',$data['images']):$data['images'];
        $order=model("Order")->where(['id'=>$data['order_id']])->find();
        $data['house_id']=$order['house_id'];
        $data['fangdong_id']=$order['fangdong_id'];

        $is_comment=model("Comment")->where(['order_id'=>$data['order_id'],'uid'=>$data['uid']])->find();
        if (!empty($is_comment)){
            return $this->fail('您已评论过,请勿重复提交评论');
        }

        $res=model("Comment")->insert($data);
        if ($res){
            model("Order")->where(['id'=>$data['order_id']])->update(['is_comment'=>1]);
            return $this->ok('评价成功');
        }else{
            return $this->fail('评价失败');
        }
    }

    //我的评价
    public function my_commment()
    {
        $uid=$this->getLoginUid();
        $user_info=model("Member")->where(['uid'=>$uid])->find();
        $user_head=model("File")->getPath($user_info['head_img']);
        $page=request()->param("page",1,'intval');
        $data=model("Comment")
            ->where(['uid'=>$uid])->order("id desc")
            ->page($page,8)
            ->select();
        if (!empty($data)){
            foreach ($data as $key=>$val){
                $data[$key]['create_time']=date("Y-m-d H:i:s",$val['create_time']);
                $data[$key]['images']=model("File")->getPaths($val['images']);
                $data[$key]['head_img']=$user_head;

            }
        }
        return $this->ok('请求成功',$data);
    }

}
